﻿using LY.DataAccess.Repository;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Interface;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LY.Business.ServiceImpl
{
    public class ArticleService : IArticleService
    {
        ArticleRepository _repo;
        public ArticleService()
        {
            _repo = new ArticleRepository();
        }

        //文章条数
        public int GetArticleCount(int menuid)
        {
            return _repo.GetArticleCount(menuid);
        }

        /// <summary>
        /// 检索文章信息
        /// </summary>
        /// <param name="search"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<C_Article> QueryArticleList(ArticleSearch search, PageView view)
        {
            return _repo.QueryArticleList(search, view);
        }
        public PagedList<C_Article> QueryListAll(ArticleSearch search, PageView view)
        {
            return _repo.QueryList(search, view);
        }
        /// <summary>
        /// 检索栏目信息
        /// </summary>
        /// <param name="search"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<C_Menu> QueryMenuList(ArticleSearch search, PageView view)
        {
            return _repo.QueryMenuList(search, view);
        }
        /// <summary>
        /// 根据栏目Id查询子栏目信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<C_Menu> QueryMenuListId(int? Id, ArticleSearch search, PageView view)
        {
            return _repo.QueryMenuListId(Id, search, view);
        }
        /// <summary>
        /// 根据ID检索文章信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public C_Article GetArticle(int? id)
        {
            return _repo.GetArticle(id);
        }
        /// <summary>
        /// 根据ID检索文章信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public C_Article GetArticleByMenu(int? sid)
        {
            return _repo.GetArticleByMenu(sid);
        }

        /// <summary>
        /// 根据ID检索文章信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<C_Article> GetArticleListByMenu(int? sid)
        {
            return _repo.GetArticleListByMenu(sid);
        }
        /// <summary>
        /// 根据栏目CODE 检索新闻 
        /// </summary>
        /// <param name="code">金融社区</param>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<C_Article> GetArticleList(string code, int top)
        {
            return _repo.GetArticleList(code,top);
        }
        /// <summary>
        /// 根据ID检索文章信息数量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetArticleCountByMenu(int? sid)
        {
            return _repo.GetArticleCountByMenu(sid);
        }
        /// <summary>
        /// 根据ID检索栏目信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public C_Menu GetMenu(int? id)
        {
            return _repo.GetMenu(id);
        }
        /// <summary>
        /// 新增或修改时保存文章信息
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public int SaveArticle(C_Article article)
        {
            if (article.ID <= 0)
            {
                //执行新增操作
                return (int)_repo.Insert(article);
            }
            else
            {
                //执行修改操作
                return (int)_repo.Update(article);
            }
        }
        /// <summary>
        /// 新增或修改时保存栏目信息
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public int SaveMenu(C_Menu menu)
        {
            //if (menu.ID <= 0)
            //{
            //    //执行新增操作
            //    return (int)_repo.Insert(menu);
            //}
            //else
            //{
            //    //执行修改操作
            //    return (int)_repo.Update(menu);
            //}
            string oldpath = "";
            string newpath = "";
            int menuSequence = 0;
            if (menu.Sequence != null)
            {
                menuSequence = Convert.ToInt32(menu.Sequence);
            }
            if (menu.ID <= 0)
            {
                //新增
                //获取父级资料
                int ParentID = Convert.ToInt32(menu.UpperID);
                C_Menu parentmodule = _repo.GetMenu(ParentID);
                menu.Layer = parentmodule.Layer + 1;
                menu.Path = parentmodule.Path + Convert.ToString(menu.ID + menuSequence * 10000 + 100000000) + ',';
                return (int)_repo.Insert(menu);
            }
            else
            {
                //编辑
                oldpath = menu.Path;
                //获取父级资料
                int ParentID = Convert.ToInt32(menu.UpperID);
                C_Menu parentmodule = _repo.GetMenu(ParentID);
                menu.Layer = parentmodule.Layer + 1;
                newpath = parentmodule.Path + Convert.ToString(menu.ID + menuSequence * 10000 + 100000000) + ',';

                UpdateSequence(oldpath, newpath);
                menu.Path = newpath;
                return (int)_repo.Update(menu);

            }
        }

        public int UpdateSequence(string oldpath, string newpath)
        {
            return _repo.UpdateSequence(oldpath, newpath);
        }
        /// <summary>
        /// 删除文章信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteArticle(int id)
        {
            return _repo.DeleteArticle(id);
        }
        /// <summary>
        /// 删除栏目信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteMenu(int id)
        {
            int inupperID = _repo.QueryUpperID(id);

            if (inupperID > 0)
            {
                return 0;
            }
            else
            {
                return _repo.DeleteMenu(id);
            }
        }

        public List<C_Menu> GetUpperMenuList(int? id)
        {
            return _repo.GetUpperMenuList(id);
        }
        /// <summary>
        /// 检查Code是否已经存在
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int GetCode(string code)
        {
            return _repo.GetCode(code);
        }
        /// <summary>
        /// 修改时，检查Code是否存在,不包含当前Code
        /// </summary>
        /// <param name="id"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public int GetCode(int id, string code)
        {
            return _repo.GetCode(id, code);
        }
        /// <summary>
        /// 根据栏目ID查找栏目下是否有文章信息
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        public int CheckArticle(int menuid)
        {
            return _repo.CheckArticle(menuid);
        }

        //文章资讯 前5
        public List<C_Article> GetArticleList(int menuid, int top)
        {
            return _repo.GetArticleList(menuid, top);
        }


        public List<C_Menu> QueryMainMenuList(int id)
        {
            return _repo.GetUpperMenuList(id);
        }

        /// <summary>
        /// 检索模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<C_Menu> GetMenuSub(int? id)
        {
            return _repo.GetMenuSub(id);
        }
    }
}
